package com.wang.friend.controller;

import com.wang.friend.domain.user.dto.UserDTO;
import com.wang.friend.domain.user.dto.UserUpdateDTO;
import com.wang.friend.domain.user.vo.LoginUserVO;
import com.wang.friend.domain.user.vo.UserVO;
import com.wang.friend.service.IUserService;
import com.wang.common.core.constants.HttpConstants;
import com.wang.common.core.controller.BaseController;
import com.wang.common.core.domain.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "C端用户功能接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Operation(summary = "发送验证码")
    @PostMapping("/sendCode")
    public R<Void> sendCode(@RequestBody UserDTO userDTO){
        return BaseController.toR(userService.sendCode(userDTO));
    }

    @Operation(summary = "根据验证码进行登录")
    @PostMapping("/code/login")
    //登录之后需要把token返回给前端
    public R<String> codeLogin(@RequestBody UserDTO userDTO){
        return R.success(userService.codeLogin(userDTO));
    }

    @Operation(summary = "用户退出登录")
    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        //@RequestHeader: 从HTTP请求头中获取指定名称的字段值
        //@RequestHeader(HttpConstants.AUTHENTICATION): 从HTTP请求头中获取名称为AUTHENTICATION的字段值 (里面存的通通常是认证信息,如JWT令牌)
        return BaseController.toR(userService.logout(token));
    }

    @Operation(summary = "查询用户信息 (头像+昵称)")
    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return userService.info(token);
    }

    @Operation(summary = "查询个人中心页面详细信息")
    @GetMapping("/detail")
    public R<UserVO> detail(){
        return R.success(userService.detail());
    }

    @Operation(summary = "编辑个人中心页面详细信息")
    @PutMapping("/edit")
    public R<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO){
        return BaseController.toR(userService.edit(userUpdateDTO));
    }

    @Operation(summary = "更新用户头像")
    @PutMapping("/head-image/update")
    public R<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO){
        return BaseController.toR(userService.updateHeadImage(userUpdateDTO.getHeadImage()));
    }
}
